<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopPaymentMethod {
	var $db;
 	var $smarty;
 	
    public function __construct($db, $smarty)  {
    	$this->db = $db;
		$this->smarty = $smarty;
    }
    
    public function CreatePaymentMethod() {
		$payment_method = @$_POST['payment_method'];
		$description = @$_POST['description'];
		if($_FILES['payment_logo']['name'] != NULL && $_FILES['payment_logo']['name'] != "") {
			$upload = new BgcmsUpload();
			$image = $_FILES['payment_logo']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_scale_file($_FILES['payment_logo']['tmp_name'], "payment", $image_type,UPLOAD_PAYMENT);
			} 
		}
		
		$payment = array('payment_method' => $payment_method,
						'payment_logo'=>$imagename,
					  'description' => $description);
		$ret = "";				 
		$ret = BWebshopPaymentMethodBean::validatePayment();
		if($ret == "") {					
			$id = BWebshopPaymentMethodBean::savePayment($this->db, $payment);

			if($id > 0) {
				$message = "Payment method ".$payment_method." has been added.";
				setcookie("message", $message,time()+5);
				header("Location: ./?module=BgcmsWebshop&sub1=payment_method");
			} else {
				$message = "Could not insert new payment method to database.";
				setcookie("message", $message,time()+5);
				header("Location: ./?module=BgcmsWebshop&sub1=payment_method&act=addpaymentmethod");
			}
			
		} else {
			setcookie("message", $ret,time()+5);
			header("Location: ./?module=BgcmsWebshop&sub1=payment_method&act=addpaymentmethod");
		}
	}
	
	public function DeletePaymentMethod() {
		$arr = array("code" => -1, "message" => _("Invalid Arguments."));
		if(isset( $_POST['id'])) {
			$payment = BWebshopPaymentMethodBean::getPaymentMethodById($this->db, $_POST['id']);
			$res = BWebshopPaymentMethodBean::deletePaymentMethod($this->db, $_POST['id']);
			@unlink("../".UPLOAD_PAYMENT.$payment->payment_logo);
			if($res) {
				$arr['code'] = 0;
				$arr['message'] = sprintf(_("Featured with Id %s has been deleted."),
										"<b>". $_POST['id'] . "</b>");
			} else {
				$arr['message'] = _("Could not delete Featured, Id: ") . $_POST['id'];
			}
		}
		echo array2json($arr);
		//we use ajax delete product, so no need to show rest of page here
	}
	
	public function EditPaymentMethod() {
		if(isset( $_POST['id'])) {
			$id = @$_POST['id'];
			$payment_method = @$_POST['payment_method'];
			$description = @$_POST['description'];
			if($_FILES['payment_logo']['name'] != NULL && $_FILES['payment_logo']['name'] != "") {
				$upload = new BgcmsUpload();
				$image = $_FILES['payment_logo']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_scale_file($_FILES['payment_logo']['tmp_name'], "payment", $image_type,UPLOAD_PAYMENT);
					@unlink("../".UPLOAD_PAYMENT.$_POST['payment_logo']);
				} 
			}
		
		$payment = array('payment_method' => $payment_method,
						'id' => $id,
						'payment_logo'=>$imagename,
					  	'description' => $description);
			$msgId = 2;
			$msg = "";				 
			$msg = BWebshopPaymentMethodBean::validatePayment();
			if($msg == "") {
				$res = BWebshopPaymentMethodBean::updatePaymentMethod($this->db, $payment);
				if($res>0) {
					$message = "Payment method ".$payment_method." has been updated.";
					setcookie("message", $message,time()+5);
					header("Location: ./?module=BgcmsWebshop&sub1=payment_method");
				}
			} else {
				//we set msgId to 99 to indicate message will load in session variable
				$message = "Could not insert new payment method to database.";
				setcookie("message", $message,time()+5);
				header("Location: ./?module=BgcmsWebshop&sub1=payment_method&act=editpaymentmethod&id=".$id);
			}
			
		}
	}
}
?>